package http_client

import (
	"context"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/parnurzeal/gorequest"
)

const (
	OPEN_API_HOST  = "open_api_host"
	OPEN_API_APPID = "open_api_appid"
	OPEN_API_KEY   = "open_api_key"
)

type OpenApiHttpClient struct {
	client *HttpClientTool
}

// NewOpenApiHttpClient
// 采购平台供应商开放服务http client工具类
func NewOpenApiHttpClient() *OpenApiHttpClient {
	return &OpenApiHttpClient{
		client: New(),
	}
}

// Get
// openApiCenter服务Get请求
func (c *OpenApiHttpClient) Get(ctx context.Context, path string, params map[string]interface{}, header map[string]string) *OpenApiHttpClient {
	url := stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_API_HOST, "")
	url += path
	params["app_id"] = stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_API_APPID, "")
	url = JoinQueryStr(url, params)

	c.client = c.client.Get(ctx, url, header)
	return c
}

// Post
// openApiCenter服务Post请求
func (c *OpenApiHttpClient) Post(ctx context.Context, path string, params map[string]interface{}, header map[string]string) *OpenApiHttpClient {
	url := stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_API_HOST, "")
	url += path
	getParams := map[string]interface{}{}
	getParams["app_id"] = stark.Configer.(stark.ApolloConfigerIface).GetStringValue(OPEN_API_APPID, "")
	url = JoinQueryStr(url, getParams)

	c.client = c.client.Post(ctx, url, params, header)
	return c
}

// EndStruct
// convert interface(v) to struct
func (c *OpenApiHttpClient) EndStruct(v interface{}) (gorequest.Response, []byte, []error) {
	return c.client.EndStruct(v)
}

type OpenApiResponse struct {
	Success   bool        `json:"success,omitempty"`
	Message   string      `json:"message,omitempty"`
	ErrorCode interface{} `json:"errorCode,omitempty"`
	Data      interface{} `json:"data,omitempty"`
}
